package beans;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import java.util.*;

//交给Spring管理，相当于<bean id="stu" class="beans.Student"/>.
//value用来指定当前类的bean-id,value这个单词可以省略。如果括号也不写，则默认为类名字小写。
//@Component(value = "stu")
@Component("stu")
//@Service
//@Controller
//@Repository

//默认是单例singleton。非单例prototype,value = 可以省略。
@Scope(value = "singleton")

//默认是饿汉false。懒汉模式：true。
@Lazy()
public class Student {

    //默认值
    private String stuNum = "100001";
    private String stuName;
    private String stuGender;
    private int stuAge;
    private Date entranceTime;

    //自动装配,默认类型匹配byType。
    //required：是否必须装配。默认true,没找到就抛出异常。false:没找到也不抛出异常。
    //也可以用于set方法。
//    @Autowired(required = false)
//    private Clazz clazz;

    //@Resource也可以声明自动装配。
    //默认先匹配属性名byName,如果没找到再匹配类型byType，如果还没找到或者匹配不止一个类型的bean则抛出异常。
    @Resource
    private Clazz clazz;

    public String getStuNum() {
        return stuNum;
    }

    //构造器之后的初始化方法，相当与bean标签的init-method="init"
    @PostConstruct
    public void init() {
        System.out.println("-----------------init");
    }

    public Clazz getClazz() {
        return clazz;
    }

    //Qualifier根据引用ref（或者叫属性名）注入。
//    @Autowired(required = false)
//    public void setClazz(@Qualifier("clazz") Clazz clazz) {
//        this.clazz = clazz;
//    }

    public void setClazz(Clazz clazz) {
        this.clazz = clazz;
    }

    //destroy-method="destroy"
    @PreDestroy
    public void destroy() {
        System.out.println("-----------------destroy");
    }

    public void setStuNum(String stuNum) {
        this.stuNum = stuNum;
    }

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public String getStuGender() {
        return stuGender;
    }

    public void setStuGender(String stuGender) {
        this.stuGender = stuGender;
    }

    public int getStuAge() {
        return stuAge;
    }

    public void setStuAge(int stuAge) {
        this.stuAge = stuAge;
    }

    public Date getEntranceTime() {
        return entranceTime;
    }

    public void setEntranceTime(Date entranceTime) {
        this.entranceTime = entranceTime;
    }

    @Override
    public String toString() {
        return "Student{" +
                "stuNum='" + stuNum + '\'' +
                ", stuName='" + stuName + '\'' +
                ", stuGender='" + stuGender + '\'' +
                ", stuAge=" + stuAge +
                ", entranceTime=" + entranceTime +
                ", clazz=" + clazz +
                '}';
    }
}
